package com.xiangyun.resource.config;

import java.security.KeyPair;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import com.xiangyun.utils.RSAUtils;

@EnableResourceServer
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true,jsr250Enabled = true)
public class HtResourceConfig extends ResourceServerConfigurerAdapter{
	
	private static final String JWTPRIVATE_KEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAJD/YAX9k1/+rhskToZE7/599LJCUtJmM8qj6uNRd4Qh7l5R+RvbLFFl9ndXQT41K1pTlTNVAuyI3CtRdfY0lytW13ZG64AZCjxcAx3uUeIvAJc54toAjfNznABMj5FAXr/M143fAfvhB2smOLxyY7is4mUzPsGVMZA9CSpLCqOXAgMBAAECgYEAisj1FybMzkBOU8pfouMPN+9FrO4QqjSK9DYLWMOcewujRgJ+Rux1MgA/daE94JYxxcZk8/o1z0N8QbY6C4vw+Gha/yvCqbF7bvo5KOFaGLEnyPvsqYEvLFSoi/VuE060x+2VIqmdyYWIGw9KTkYB53pGrL4W2WJTDoWduVCFyPECQQDKy1CmtUF8W6+GFO9Inr791I+oKUTxo++I86VICPLPa8tbae+tc2uPMQ8nmiXr4SmbdlVAIndUZuXY3xZAk3rJAkEAtwolJjrmMhBgAsbGc7P0GIQ1S2Kss0zNmDFCiRtQHEiFAz9xqHsYujUimvV1fPKHUDZlflId/DpwHHWZLQX7XwJANuy2HVC+OcZDVddZr5t/MxRdDDiRObki6T7EJyx1RwR1AX9BsQKvwQqShjFTz/0d3cNA96veEfgEjKZvSnInMQJBAJEM4hmkrU1ZOqyYlyWToslW4+EEpjVElNyZlM+QLHDtazMCsPBB9HuDphPElsFYdLC1GsL7EjYSnUuASc2rIuMCQQCov6OfTONon7yRH9B0OMbImCcFNu+ivxqwUnZIONPKsSgivdpU2NqsZaYpUvOIkpPu3a/9LuPJSCarFlS7sv1y";

	private static final String JWTPUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCQ/2AF/ZNf/q4bJE6GRO/+ffSyQlLSZjPKo+rjUXeEIe5eUfkb2yxRZfZ3V0E+NStaU5UzVQLsiNwrUXX2NJcrVtd2RuuAGQo8XAMd7lHiLwCXOeLaAI3zc5wATI+RQF6/zNeN3wH74QdrJji8cmO4rOJlMz7BlTGQPQkqSwqjlwIDAQAB";

	
	@Override
	public void configure(HttpSecurity http) throws Exception {
		// TODO Auto-generated method stub
		http.authorizeRequests().antMatchers("/**").authenticated();
	}
	
	@Bean
	public TokenStore tokenStore() {
		return new JwtTokenStore(tokenConverter());
	}

	/**
	 * jwt令牌增强器具体作用不知道……
	 * @return
	 */
	public JwtAccessTokenConverter tokenConverter() {
		JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
		KeyPair keyPair = null;
		try {
			keyPair = new KeyPair(RSAUtils.getPublicKey(JWTPUBLIC_KEY), RSAUtils.getPrivateKey(JWTPRIVATE_KEY));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		converter.setKeyPair(keyPair);
		return converter;
	}
	
	@Override
	public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
		// TODO Auto-generated method stub
		resources.tokenStore(tokenStore())
		.stateless(true);
	}
}
